package com.example.materialtest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawerLayout;
    private SwipeRefreshLayout swipeRefresh;
    private Fruit[] fruits = {new Fruit("Apple", R.drawable.fruits),
            new Fruit("Banana", R.drawable.fruits),
            new Fruit("Orange", R.drawable.fruits),
            new Fruit("Watermelon", R.drawable.fruits),
            new Fruit("Pear", R.drawable.fruits),
            new Fruit("Grape", R.drawable.fruits),
            new Fruit("Pineapple", R.drawable.fruits),
            new Fruit("Strawberry",R.drawable.fruits),
            new Fruit("Cherry", R.drawable.fruits),
            new Fruit("Mango", R.drawable.fruits)};
    private List<Fruit> fruitList = new ArrayList<>();
    private FruitAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // 获得ActionBar实例
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            //设置导航栏图标
            // actionBar.setHomeAsUpIndicator(R.drawable.ic_nav);
        }
        // 设置导航栏图标
        toolbar.setNavigationIcon(R.drawable.ic_nav);
        // 为这个按钮设置点击事件
        // toolbar.setNavigationOnClickListener();

        // 获取DrawerLayout控件
        drawerLayout = findViewById(R.id.drawer_layout);
        // 获取NavigationView
        NavigationView navigationView = findViewById(R.id.nav_view);
        //设置默认选中第一个
        navigationView.setCheckedItem(R.id.nav_phone);
        // 菜单项监听器
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            // 用户点击了任意菜单项时，就会回调到onNavigationItemSelected() 方法中
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                // 关闭滑动菜单
                drawerLayout.closeDrawers();
                return true;
            }
        });

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "Data deleted", Snackbar.LENGTH_SHORT)
                        .setAction("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(MainActivity.this, "数据恢复",
                                        Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        });

        //recyclerView 和卡片
        initFruits();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rcycler_view);
        //每一行中会有两列数据
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new FruitAdapter(fruitList);
        recyclerView.setAdapter(adapter);

        //刷新
        swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
        //来设置下拉刷新进度条的颜色
        swipeRefresh.setColorSchemeResources(R.color.purple_200);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.
                OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshFruits();
            }
        });
    }
    private void refreshFruits() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initFruits();
                        adapter.notifyDataSetChanged();
                        //2秒之后停止刷新
                        swipeRefresh.setRefreshing(false);
                    }
                });
            }
        }).start();
    }
    private void initFruits() {
        fruitList.clear();
        for (int i = 0; i < 50; i++) {
            Random random = new Random();
            int index = random.nextInt(fruits.length);
            fruitList.add(fruits[index]);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // 加载了toolbar.xml这个菜单文件
        getMenuInflater().inflate(R.menu.toolbar,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //处理各个按钮的点击事件
        switch (item.getItemId()){
            // HomeAsUp按钮的id永远都是android.R.id.home
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                break;
            case R.id.upload:
                Toast.makeText(this,"upload",Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(this,"delete",Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting1:
                Toast.makeText(this,"setting1",Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting2:
                Toast.makeText(this,"setting2",Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
    }
}